package com.ruoyi.web.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.domain.SysSingleQuestion;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTestPaper;
import com.ruoyi.system.service.ISysTestPaperService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考题管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-25
 */
@Controller
@RequestMapping("/system/sysTestPaper")
public class SysTestPaperController extends BaseController
{
    private String prefix = "system/sysTestPaper";

    @Autowired
    private ISysTestPaperService sysTestPaperService;

    @RequiresPermissions("system:sysTestPaper:view")
    @GetMapping()
    public String sysTestPaper()
    {
        return prefix + "/sysTestPaper";
    }

    /**
     * 查询考题管理列表
     */
    @RequiresPermissions("system:sysTestPaper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTestPaper sysTestPaper)
    {
        startPage();
        List<SysTestPaper> list = sysTestPaperService.selectSysTestPaperList(sysTestPaper);
        return getDataTable(list);
    }

    /**
     * 导出考题管理列表
     */
    @RequiresPermissions("system:sysTestPaper:export")
    @Log(title = "考题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTestPaper sysTestPaper)
    {
        List<SysTestPaper> list = sysTestPaperService.selectSysTestPaperList(sysTestPaper);
        ExcelUtil<SysTestPaper> util = new ExcelUtil<SysTestPaper>(SysTestPaper.class);
        return util.exportExcel(list, "考题管理数据");
    }

    /**
     * 新增考题管理
     */
    @RequiresPermissions("system:sysTestPaper:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考题管理
     */
    @RequiresPermissions("system:sysTestPaper:add")
    @Log(title = "考题管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTestPaper sysTestPaper)
    {
        return toAjax(sysTestPaperService.insertSysTestPaper(sysTestPaper));
    }

    /**
     * 修改考题管理
     */
    @RequiresPermissions("system:sysTestPaper:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTestPaper sysTestPaper = sysTestPaperService.selectSysTestPaperById(id);
        mmap.put("sysTestPaper", sysTestPaper);
        return prefix + "/edit";
    }

    /**
     * 修改考题管理
     */
    @RequiresPermissions("system:sysTestPaper:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysTestPaper sysTestPaper = sysTestPaperService.selectSysTestPaperById(id);
        String extendedField = sysTestPaper.getExtendedField();
        List<SysSingleQuestion> sysSingleQuestions = JSON.parseArray(extendedField, SysSingleQuestion.class);
        sysTestPaper.setSysSingleQuestions(sysSingleQuestions);
        mmap.put("sysTestPaper", sysTestPaper);
        return prefix + "/detail";
    }

    /**
     * 修改保存考题管理
     */
    @RequiresPermissions("system:sysTestPaper:edit")
    @Log(title = "考题管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTestPaper sysTestPaper)
    {
        return toAjax(sysTestPaperService.updateSysTestPaper(sysTestPaper));
    }

    /**
     * 删除考题管理
     */
    @RequiresPermissions("system:sysTestPaper:remove")
    @Log(title = "考题管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTestPaperService.deleteSysTestPaperByIds(ids));
    }

    /**
     * 发布考试
     */
    @RequiresPermissions("system:sysTestPaper:publish")
    @PostMapping( "/publish")
    @ResponseBody
    public AjaxResult publish(String id)
    {
        sysTestPaperService.publish(id);
        return toAjax(1);
    }
}
